package org.handsomezhi.shortlink.project.mq.producer;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.UUID;

/**
 * 短链接监控状态保存消息队列生产者
 * 公众号：马丁玩编程，回复：加群，添加马哥微信（备注：link）获取项目资料
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ShortlinkRabbitMqProducer {

    private final RabbitTemplate rabbitTemplate;

    @Value("${spring.rabbitmq.template.exchange}")
    private String statsSaveExchange;
    @Value("${spring.rabbitmq.template.routing-key}")
    private String directKey;

    /**
     * 发送延迟消费短链接统计
     */
    public void send(Map<String, String> producerMap) {
        String keys = UUID.randomUUID().toString();
        producerMap.put("keys", keys);

        rabbitTemplate.convertAndSend(statsSaveExchange,directKey,producerMap);

//        Message<Map<String, String>> build = MessageBuilder
//                .withPayload(producerMap)
//                .setHeader(MessageConst.PROPERTY_KEYS, keys)
//                .build();
//        SendResult sendResult;
//        try {
//            sendResult = rocketMQTemplate.syncSend(statsSaveTopic, build, 2000L);
//            log.info("[消息访问统计监控] 消息发送结果：{}，消息ID：{}，消息Keys：{}", sendResult.getSendStatus(), sendResult.getMsgId(), keys);
//        } catch (Throwable ex) {
//            log.error("[消息访问统计监控] 消息发送失败，消息体：{}", JSON.toJSONString(producerMap), ex);
//            // 自定义行为...
//        }
    }
}